﻿using Manon.Autofac;
using Manon.ConfigCenter.Web.ApplicationCore.Dtos.Project;
using Manon.ConfigCenter.Web.ApplicationCore.Dtos.ProjectConfig;
using Manon.ConfigCenter.Web.ApplicationCore.Interfaces;
using Manon.Core.Commons;
using Manon.Repository.Collections;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Manon.ConfigCenter.Web.Controllers
{
    public class ProjectController : Controller
    {



        public IActionResult Index()
        {
            return View();
        }


        public IActionResult Edit()
        {
            return View();
        }

        public IActionResult Detail()
        {
            return View();
        }


        public IActionResult History()
        {
            return View();
        }

        public IActionResult ConfigSettings()
        {
            return View();
        }

        public IActionResult ConfigDbSettings()
        {
            return View();
        }

        public IActionResult OnlineConfig()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public async Task<IPagedList<ProjectsOutput>> GetProjectList([FromBody] GetProjectListInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();

            var result = await project.GetProjectPageListAsync(Input);

            return result;
        }

        /// <summary>
        /// 获取线上已经发布的配置
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="EnvId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet("GetOnlineConfigById")]
        public async Task<string> GetOnlineConfigById(int ProjectId, int EnvId)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetOnlineConfigById(ProjectId, EnvId);

            return result;
        }

        [Authorize]
        [HttpGet("GetProjectSimpleList")]
        public async Task<List<ProjectSimpleOutput>> GetProjectSimpleList()
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetProjectSimpleListAsync();

            return result;
        }
        [Authorize]
        [HttpPost]
        public async Task<Result> SaveProject([FromBody] SaveProjectInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.SaveProject(Input);
            return result;
        }
        [Authorize]
        [HttpGet]
        public async Task<ProjectsOutput> GetProject(int Id)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetProject(Id);
            return result;
        }
        [Authorize]
        [HttpPost]
        public async Task<Result> SetEnabled([FromBody] SetEnabledInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.SetEnabled(Input);
            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<Result> SaveProjectSettings([FromBody] SaveProjectSettingsInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.SaveProjectSettings(Input);
            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<ProjectConfigOutput> GetLastConfigSettings([FromBody] GetLastConfigSettingsInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetLastConfigSettings(Input);
            return result;
        }


        [Authorize]
        [HttpPost]
        public async Task<Result> SetConfigPass([FromBody] SetConfigPassInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.SetConfigPass(Input);
            return result;
        }
        [Authorize]
        [HttpPost]
        public async Task<Result> SetConfigUnPass([FromBody] SetConfigUnPassInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.SetConfigUnPass(Input);
            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<Result> PublishConfig([FromBody] PublishConfigInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.PublishConfig(Input);
            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<Result> FinishConfig([FromBody] PublishConfigInput Input)
        {
            var project = Resolver.Resolve<IProjectService>();
            var result = await project.FinishConfig(Input);
            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<IPagedList<ProjectsConfigHistoryOutput>> GetProjectHistoryPageList([FromBody] GetProjectHistoryPageListInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetProjectHistoryPageList(Input);

            return result;
        }

        [Authorize]
        [HttpGet]
        public async Task<ProjectsConfigHistoryOutput> GetProjectHistoryById(int HistoryId)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetProjectHistoryById(HistoryId);

            return result;
        }



        [Authorize]
        [HttpPost]
        public async Task<IPagedList<GetProjectSecretPageListOutput>> GetProjectSecretPageList([FromBody] GetProjectSecretPageListInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.GetProjectSecretPageListAsync(Input);

            return result;
        }

        [Authorize]
        [HttpPost]
        public async Task<Result> CreateEnvProjectSecret([FromBody] CreateEnvProjectSecretInput Input)
        {

            var project = Resolver.Resolve<IProjectService>();
            var result = await project.CreateEnvProjectSecret(Input);

            return result;
        }
    }
}